package com.jzo2o.orders.dispatch.strategys;

import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.orders.dispatch.model.dto.ServeProviderDTO;

import java.util.List;

/**
 * @author Mr.M
 * @version 1.0
 * @description TODO
 * @date 2023/12/30 16:25
 */
public abstract class AbstractProcessRule implements IProcessRule {

    private IProcessRule next;

    public AbstractProcessRule(IProcessRule next){
        this.next = next;
    }

    public abstract List<ServeProviderDTO> doFilter(List<ServeProviderDTO> serveProviderDTOS);

    @Override
    public List<ServeProviderDTO> filter(List<ServeProviderDTO> serveProviderDTOS) {
        //根据距离排序，找到最近的师傅
        List<ServeProviderDTO> result = doFilter(serveProviderDTOS);

        //如果找到了多个师傅，继续匹配下一个规则
        if (CollUtils.isNotEmpty(result) && result.size() > 1 && next != null) {
            return next.filter(result);
        } else {
            //当前没有匹配到或只匹配到一个师傅，直接返回，不再向下匹配规则了
            return result;
        }
    }

    @Override
    public IProcessRule next() {
        return next;
    }
}
